package com.heer.gymnasium.module.store.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.SignHelper;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.api.constants.IConstants;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.common.beans.GlobeBean;
import com.heer.gymnasium.module.fellow.views.InitiateFellowActivity;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.module.main.beans.CategoryBean;
import com.heer.gymnasium.module.reservation.views.ReservationListActivity;
import com.heer.gymnasium.module.store.beans.AreaBean;
import com.heer.gymnasium.module.store.beans.StoreBean;
import com.heer.gymnasium.module.store.beans.StoreResponseBean;
import com.heer.gymnasium.util.Session;
import com.heer.imagepick.DensityUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.zhihuiyun.library.commonadapter.QuickAdapter;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;
import com.zhihuiyun.library.utils.SPUtils;
import com.zhihuiyun.library.view.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnItemSelected;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/2/26
 */

public class SelectStoreActivity extends BaseRefreshActivity {

    private static final int RES_ID_LAYOUT = R.layout.layout_select_store;
    private static final int RES_ID_SPINNER = R.id.layout_select_store_spinner;
    private static final int RES_ID_REFRESH = R.id.layout_select_store_refresh_srl;
    private static final int RES_ID_LIST = R.id.layout_select_store_list;
    //item
    private static final int RES_ID_ITEM = R.layout.item_select_store;
    private static final int RES_ID_ITEM_ICON = R.id.item_select_store_store_icon_iv;
    private static final int RES_ID_ITEM_RATINGBAR = R.id.item_select_store_ratingbar;
    private static final int RES_ID_ITEM_DISTANCE = R.id.item_select_store_distance_tv;
    private static final int RES_ID_ITEM_STORENAME = R.id.item_select_store_store_name_tv;
    private static final int RES_ID_ITEM_ICONS = R.id.item_select_store_icons_ll;
    private static final int RES_ID_ITEM_ADDRESS = R.id.item_select_store_store_address_tv;
    private static final int RES_ID_ITEM_LIST = R.id.item_select_store_list;
    private static final int RES_ID_ITEM_MORE = R.id.item_select_store_more_iv;


    private static final int RES_ID_ITEM_ITEM = R.layout.item_select_store_tag;
    private static final int RES_ID_ITEM_TYPE_IV = R.id.item_select_store_tag_type_iv;
    private static final int RES_ID_ITEM_TYPE_TV = R.id.item_select_store_tag_type_tv;
    private static final int RES_ID_ITEM_ORDER = R.id.item_select_store_tag_order_tv;

    private static final int RES_ID_ITEM_SPINNER_LAYOUT = R.layout.item_dropdown;
    private static final int RES_ID_ITEM_SPINNER_DROP_DOWN_LAYOUT = android.R.layout.simple_dropdown_item_1line;
    private static final int RES_ID_ITEM_SPINNER_TV = R.id.item_dropdown_type_tv;


    @BindView(RES_ID_SPINNER)
    Spinner spinner;
    @BindView(RES_ID_LIST)
    MyListView listView;

    private ArrayAdapter spinnerAdapter;
    private Activity $this;
    private List<StoreBean> data;
    private QuickTypeAdapter<StoreBean> adapter;
    private List<Boolean> moreStatus;

    private String DEFAULT_AREA;
    private List<AreaBean> areaList;
    private ArrayAdapter<AreaBean> areaAdapter;
    private String areaCode = "";
    private String categoryId;
    private int reservationType;


    @Override
    protected void init() {
        super.init();
        $this = this;
        DEFAULT_AREA = "店铺区域";
        areaList = new ArrayList<>();
        setContentView(RES_ID_LAYOUT);
        setRefreshResId(RES_ID_REFRESH);
        categoryId = getIntent().getStringExtra(EXTRA_ID);
        reservationType = getIntent().getIntExtra(EXTRA_TYPE, Type.RESERVATION_TYPE_MATCH);
    }

    @Override
    protected void initView() {
        super.initView();
        data = new ArrayList<>();
        moreStatus = new ArrayList<>();
        adapter = new QuickTypeAdapter<StoreBean>(this, data, RES_ID_ITEM) {
            @Override
            public void bindData4View(final ViewHolder holder, final StoreBean data, int type, final int pos) {
                holder.setText(RES_ID_ITEM_STORENAME, data.getStadiumname());
                holder.setText(RES_ID_ITEM_ADDRESS, data.getStadiumaddress());
                holder.setText(RES_ID_ITEM_DISTANCE, data.getDistance());
                LinearLayout linearLayout = holder.getView(RES_ID_ITEM_ICONS);
                linearLayout.removeAllViews();
                List<CategoryBean> temp = new ArrayList<>();
                if (data.getPlanList() != null && data.getPlanList().size() > 3) {
                    holder.setVisibility(RES_ID_ITEM_MORE, View.VISIBLE);
                    if (moreStatus.get(pos)) {
                        holder.setBg(RES_ID_ITEM_MORE, R.drawable.ic_tow_up);
                        temp.addAll(data.getPlanList());
                    } else {
                        temp.addAll(data.getPlanList().subList(0, 3));
                        holder.setBg(RES_ID_ITEM_MORE, R.drawable.ic_tow_down);
                    }
                } else {
                    holder.setVisibility(RES_ID_ITEM_MORE, View.GONE);
                    temp.addAll(data.getPlanList());
                }
                holder.setLis(RES_ID_ITEM_MORE, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        holder.setBg(RES_ID_ITEM_MORE, moreStatus.get(pos) ? R.drawable.ic_tow_up : R.drawable.ic_tow_down);
                        if (moreStatus.get(pos)) {
                            moreStatus.set(pos, false);
                        } else {
                            moreStatus.set(pos, true);
                        }
                        notifyDataSetChanged();
                    }
                });
                ListView listView = holder.getView(RES_ID_ITEM_LIST);
                listView.setAdapter(new QuickAdapter<CategoryBean>($this, RES_ID_ITEM_ITEM, temp) {
                    @Override
                    public void bindData4View(ViewHolder holder, final CategoryBean data, int pos) {
                        holder.setText(RES_ID_ITEM_TYPE_TV, data.getTitle());
                        holder.setImg($this,RES_ID_ITEM_TYPE_IV, data.getIcon());
                        holder.setLis(RES_ID_ITEM_ORDER, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ReservationListActivity.startActivity($this,data.getId(),reservationType);
                            }
                        });
                    }
                });
            }
        };
        listView.setAdapter(adapter);

        areaList.add(new AreaBean(DEFAULT_AREA));
        areaAdapter = new ArrayAdapter<AreaBean>(this, RES_ID_ITEM_SPINNER_LAYOUT, areaList) {
            @Override
            public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                convertView = getLayoutInflater().inflate(RES_ID_ITEM_SPINNER_DROP_DOWN_LAYOUT, null);
                TextView tv = (TextView) convertView;
                tv.setText(getItem(position).getArea());
                LinearLayout view = new LinearLayout($this);
                view.addView(convertView);
                view.setOrientation(LinearLayout.VERTICAL);
                ImageView imageView = new ImageView($this);
                imageView.setBackgroundResource(R.color.bg_color);
                imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
                view.addView(imageView);
                view.setPadding(10, 10, 10, 10);
                return view;
            }

            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                convertView = getLayoutInflater().inflate(RES_ID_ITEM_SPINNER_LAYOUT, null);
                TextView tv = (TextView) convertView.findViewById(RES_ID_ITEM_SPINNER_TV);
                tv.setText(getItem(position).getArea());
                return convertView;
            }
        };
        areaAdapter.setDropDownViewResource(RES_ID_ITEM_SPINNER_DROP_DOWN_LAYOUT);
        spinner.setAdapter(areaAdapter);
        getAreaList();
    }

    @OnItemSelected(RES_ID_SPINNER)
    public void onItemSelectedForArea(View view, int i) {
        if (spinner.getSelectedItemPosition() == 0) {
            areaCode = "";
        } else {
            areaCode = ((AreaBean) spinner.getSelectedItem()).getAdcode();
        }
        layoutRefresh.setDirection(SwipyRefreshLayoutDirection.BOTH);
        loadData(false);
    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        getYPStadiumList(loadMore);
    }


    //获取区域列表
    private void getAreaList() {
        SignHelper signHelper = new SignHelper("");
        mAppHelper.apiService().getAreaList(signHelper.GetParameter("key")).enqueue(new BaseCallback<ApiResponse<List<AreaBean>>>() {
            @Override
            public void onSuccess(Call<ApiResponse<List<AreaBean>>> call, Response<ApiResponse<List<AreaBean>>> response) {
                areaList.clear();
                areaList.add(new AreaBean(DEFAULT_AREA));
                areaList.addAll(response.body().getData());
                areaAdapter.notifyDataSetChanged();
            }

            @Override
            public void onTokenTimeOut() {
            }
        });
    }

    private void getYPStadiumList(final boolean loadMore){
        mAppHelper.apiService().getYPStadiumList(SPUtils.get(this, SPUtils.SP_LA, IConstants.LA).toString(),
                SPUtils.get(this, SPUtils.SP_LO, IConstants.LO).toString(),areaCode,categoryId).enqueue(new BaseCallback<ApiResponse<StoreResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<StoreResponseBean>> call, Response<ApiResponse<StoreResponseBean>> response) {
                layoutRefresh.setRefreshing(false);
                if (!loadMore){
                    pageNo = 1;
                    data.clear();
                }else{
                    pageNo++;
                }
                data.addAll(StoreBean.dataChange(response.body().getData().getData()));
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Call<ApiResponse<StoreResponseBean>> call, Throwable t) {
                super.onError(call, t);
                layoutRefresh.setRefreshing(false);
            }

            @Override
            public void onTokenTimeOut() {
                layoutRefresh.setRefreshing(false);
                super.onTokenTimeOut();
            }
        });
    }


    public static void startActivity(Context context,String categoryId,int reservationType) {
        Intent mIntent = new Intent(context, SelectStoreActivity.class);
        mIntent.putExtra(EXTRA_ID,categoryId);
        mIntent.putExtra(EXTRA_TYPE,reservationType);
        context.startActivity(mIntent);
    }
}
